package XianChengChi;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 创建线程的方式四：使用线程池
 */
public class Demo1 {
    public static void main(String[] args) {
        //1.提供指定线程数量的线程池service
        ExecutorService service = Executors.newFixedThreadPool(10);

        //设置线程池的属性，由于接口都是常量，只能通过实现类的对象设置
        //向下强转
        ThreadPoolExecutor poolService = (ThreadPoolExecutor) service;
        poolService.setCorePoolSize(15);//设置线程数量
//        poolService.setKeepAliveTime(, );//线程结束后活跃时间

        //2.提供指定的线程操作，需要提供实现Runnable接口或Callable接口的实现类对象
        service.execute(new AA());//适合配合Runnable使用
        service.execute(new BB());
//        service.submit();//适合配合Callable使用

        //3.关闭线程池
        service.shutdown();
    }
}
class AA implements  Runnable{
    @Override
    public void run() {
        for (int i = 0; i <=100 ; i++) {
            if(i%2==0){
                System.out.println(java.lang.Thread.currentThread().getName()+":"+i);
            }
        }
    }
}
class BB implements  Runnable{
    @Override
    public void run() {
        for (int i = 0; i <=100 ; i++) {
            if(i%2!=0){
                System.out.println(java.lang.Thread.currentThread().getName()+":"+i);
            }
        }
    }
}